package top.wshape1.shiziapi.uaa.json.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;

import java.io.IOException;
import java.util.Map;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-08-31
 */

public class OAuth2AuthorizationRequestJsonSerializer extends JsonSerializer<OAuth2AuthorizationRequest> {

    @Override
    public void serialize(OAuth2AuthorizationRequest value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeStartObject();

        gen.writeStringField("authorizationUri", value.getAuthorizationUri());
        gen.writeStringField("clientId", value.getClientId());
        gen.writeStringField("redirectUri", value.getRedirectUri());
        gen.writeStringField("state", value.getState());
        gen.writeStringField("authorizationRequestUri", value.getAuthorizationRequestUri());

        gen.writeFieldName("scopes");
        gen.writeStartArray();
        for (String scope : value.getScopes()) {
            gen.writeString(scope);
        }
        gen.writeEndArray();

        gen.writeFieldName("additionalParameters");
        gen.writeStartObject();
        for (Map.Entry<String, Object> entry : value.getAdditionalParameters().entrySet()) {
            gen.writeStringField(entry.getKey(), entry.getValue().toString());
        }
        gen.writeEndObject();

        gen.writeFieldName("attributes");
        gen.writeStartObject();
        for (Map.Entry<String, Object> entry : value.getAttributes().entrySet()) {
            gen.writeStringField(entry.getKey(), entry.getValue().toString());
        }
        gen.writeEndObject();

//            gen.writeFieldName("authorizationGrantType");
//            gen.writeStartObject();
//            gen.writeStringField("value", value.getGrantType().getValue());
//            gen.writeEndObject();
//
//            gen.writeFieldName("responseType");
//            gen.writeStartObject();
//            gen.writeStringField("value", value.getResponseType().getValue());
//            gen.writeEndObject();

        gen.writeEndObject();
    }

}
